# coding=utf8
from django.db import models
from django.contrib.auth.models import User

class Category(models.Model):
    name = models.CharField(max_length=100, verbose_name="名称")
    slug = models.SlugField(null=True,verbose_name="缩略名", help_text="请使用拼音字母，用来组成频道的URL链接，比如对于品牌频道，http://www.xmdj123.com/channel/brand/")
    order = models.SmallIntegerField(null=True, blank=True, default=0, verbose_name="序号")
    editors = models.ManyToManyField(User, null=True, verbose_name="编辑")
    parent = models.ForeignKey('self', blank=True, null=True, verbose_name="上级频道")

    class Meta:
        verbose_name = "频道"
        verbose_name_plural="频道"
        ordering = ['order']

    def articles(self):
        l = list(self.objects.order_by('-created'))
        return l

    def __unicode__(self):
        return self.name

class Article(models.Model):
    STATUS_CHOICES = (
        (1, "已提交"),
        (2, "已发布"),
        (-1, "已删除"),
    )
    title = models.CharField(max_length=200, verbose_name="标题")
    source = models.CharField(blank=True,max_length=30, verbose_name="来源")
    summary = models.CharField(blank=True, max_length=200, verbose_name="摘要")
    content = models.TextField(verbose_name="内容")
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=1, verbose_name="文章状态")
    category = models.ForeignKey(Category, verbose_name="频道")
    editor = models.ForeignKey(User, verbose_name="网站编辑")
    headline_image = models.ImageField(upload_to="news/%Y/%m/%d", blank=True, verbose_name="头条图片")
    is_headline = models.BooleanField(default=False,verbose_name="头条新闻",help_text="是否为头条新闻")
    created = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "文章"
        verbose_name_plural='文章'
        ordering = ['-updated']
        permissions = (
            ("make_published", "发布文章"),
        )

#    def summary(self):
#        if len(self.content) > 100:
#            return '%s ...' % self.content[0:100]

    def __unicode__(self):
        return self.title

